//
//  ViewController.m
//  15---Homework15网易新闻
//
//  Created by 冀永金 on 16/8/3.
//  Copyright © 2016年 冀永金. All rights reserved.
//

#import "ViewController.h"
#import "TitleCollectionViewCell.h"
#import "SelfSizeTool.h"
#import "ContentCollectionViewCell.h"
#import "TitleModel.h"
#define SCREEN_WIDTH  [UIScreen mainScreen].bounds.size.width
#define SCREEN_HEIGHT [UIScreen mainScreen].bounds.size.height
@interface ViewController ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
@property (nonatomic, strong) NSMutableArray *titleArray;
@property (nonatomic, strong) UICollectionView * smallCollection;
@property (nonatomic, strong) UICollectionView *bigCollection;
@end

@implementation ViewController
- (void)viewDidLoad {
    [super viewDidLoad];
       [self configSubViews];
    [self handleData];
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.navigationController.navigationBar.translucent = NO;
    

    NSLog(@"++++++++++++++++++==%@", NSStringFromCGRect(self.bigCollection.frame));
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:0.91 green:0.26 blue:0.26 alpha:1.00];
    self.navigationItem.title = @"網易";
    self.navigationController.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName:[UIColor whiteColor],NSFontAttributeName:[UIFont systemFontOfSize:18]};
    
    // Do any additional setup after loading the view, typically from a nib.
}
//http://c.m.163.com/nc/topicset/android/subscribe/manage/listspecial.html
- (void)configSubViews {
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    layout.sectionInset = UIEdgeInsetsMake(5, 5, 5, 5);
    layout.minimumInteritemSpacing = 40;
   //    layout.minimumLineSpacing = 0;
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    _smallCollection = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 50) collectionViewLayout:layout];
    [self.view addSubview:_smallCollection];
    
    _smallCollection.delegate = self;
    _smallCollection.dataSource = self;
//    _smallCollection.backgroundColor = [UIColor whiteColor];
    _smallCollection.showsHorizontalScrollIndicator = NO;
    [_smallCollection registerClass:[TitleCollectionViewCell class] forCellWithReuseIdentifier:@"poolOfSmall"];
    _smallCollection.backgroundColor = [UIColor brownColor];
    
    UICollectionViewFlowLayout *layout1 = [[UICollectionViewFlowLayout alloc]init];
   // layout1.itemSize = CGSizeMake(self.view.bounds.size.width, self.view.bounds.size.height - 50);
   
    layout1.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    layout1.minimumInteritemSpacing = 0;
    layout1.minimumLineSpacing = 0;
    layout1.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    self.bigCollection = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 50, self.view.bounds.size.width, SCREEN_HEIGHT - 50 - 64) collectionViewLayout:layout1];
    [self.view addSubview:_bigCollection];
    _bigCollection.pagingEnabled = YES;
   _bigCollection.delegate = self;
   _bigCollection.dataSource = self;
    _bigCollection.backgroundColor = [UIColor redColor];
   // bigCollection.backgroundColor = [UIColor colorWithRed:arc4random() % 256 / 255.0f green:arc4random() % 256 / 255.0f blue:arc4random() % 256 / 255.0f alpha:1.0f];
    [_bigCollection registerClass:[ContentCollectionViewCell class] forCellWithReuseIdentifier:@"poolOfContent"];
}
- (void)handleData {
    NSURLSession *urlSession = [NSURLSession sharedSession];
    NSMutableURLRequest *requestTitle = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://c.m.163.com/nc/topicset/android/subscribe/manage/listspecial.html"]];
    requestTitle.HTTPMethod = @"GET";
    NSURLSessionDataTask *dataTask = [urlSession dataTaskWithRequest:requestTitle completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        dispatch_async(dispatch_get_main_queue(), ^{
            self.titleArray = [NSMutableArray array];
            NSError *error = nil;
            NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];
            NSArray *list = dic[@"tList"];
            for (NSDictionary *dic in list) {
                NSString *str = dic[@"tname"];
                [self.titleArray addObject:str];
            }
            [self.smallCollection reloadData];
        });
    }];
    [dataTask resume];
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    if (collectionView == _smallCollection) {
        return self.titleArray.count;
        
    }else {
         return self.titleArray.count;
    }
}
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    if (collectionView == _smallCollection ) {
        
        TitleCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"poolOfSmall" forIndexPath:indexPath];
        cell.label.text = self.titleArray[indexPath.row];
        //cell.backgroundColor = [UIColor redColor];
       // cell.label.textAlignment = 1;
        return cell;
    }else {
        ContentCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"poolOfContent" forIndexPath:indexPath];
        cell.backgroundColor =  [UIColor colorWithRed:arc4random() % 256 / 255.0f green:arc4random() % 256 / 255.0f blue:arc4random() % 256 / 255.0f alpha:1.0f];
        return cell;
    }
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    if (collectionView == _smallCollection) {
        NSString *str = self.titleArray[indexPath.row];
        CGFloat width = [SelfSizeTool selfSizeWidthString:str height:40 font:[UIFont systemFontOfSize:17]];
        //[self.smallCollection reloadData];
        return CGSizeMake(width, 40);
    } else {
        //self.edgesForExtendedLayout =UIRectEdgeNone;
        //self.automaticallyAdjustsScrollViewInsets = NO;
         NSLog(@"____________________%@", NSStringFromCGRect(self.view.frame));
        return CGSizeMake(self.view.bounds.size.width,  SCREEN_HEIGHT - 50 - 64);
    }
    return CGSizeZero;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
